package hk.polyu;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;
import javax.swing.event.*;


public class MainUI {

	// Tab String
    final static String MONITORPANEL = "Localization";
    final static String PADPANEL = "Cushion Pad";
    final static String SETTINGPANEL = "Setting";
    
    static LocalizationPanel card1 = new LocalizationPanel();
    static CushionPadPanel card2 = new CushionPadPanel();
    static SettingPanel card3 = new SettingPanel();
    
    final static int extraWindowWidth = 100;

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("UIO Wheel Chair");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JTabbedPane tabbedPane = new JTabbedPane();

        //Create the "cards".
        tabbedPane.addTab(MONITORPANEL, card1);
        tabbedPane.addTab(PADPANEL, card2);
        tabbedPane.addTab(SETTINGPANEL, card3);
        
        tabbedPane.addChangeListener(new ChangeListener(){ 
            public void stateChanged(ChangeEvent e){ 
            	JTabbedPane tabbedPane = (JTabbedPane)e.getSource(); 
            	int selectedIndex = tabbedPane.getSelectedIndex(); 
            	if(selectedIndex == 1){ 
            		//card2.updateChart();
            	}
            }
        });
        // add top Image
        JPanel pTop = new JPanel();
        
        // Add Top Start / Stop Button
        final JButton st = new JButton("Start");
        final JButton ed = new JButton("End");
        
        st.addActionListener(new ActionListener() {
		    	public void actionPerformed(ActionEvent arg0) 
		    	{
		    		/*
		    		DataReceiver.instance().start(1);
		    		
		    		st.setEnabled(false);
		    		ed.setEnabled(true);
		    		*/
		    		DBWrapper.instance().appendRandom();
		    	}
        });

        ed.addActionListener(new ActionListener() {
		    	public void actionPerformed(ActionEvent arg0) 
		    	{
		    		DataReceiver.instance().stop();
		    		
		    		st.setEnabled(true);
		    		ed.setEnabled(false);
		    	}
        });
        
        ed.setEnabled(false);
        pTop.add(st);
        pTop.add(ed);
        
        frame.add(pTop, BorderLayout.NORTH);
        
        // add bottom
        frame.add(tabbedPane, BorderLayout.CENTER);

        //Display the window.
        frame.setSize(1024, 768);
        frame.setVisible(true);
        
        // add event handler for sensor
        DataReceiver.instance().addDataReceiveListener(new DataReceiveListener() {
			
			@Override
			public void dataReceived(DataReceiveEvent e) {
				SensorData d = e.getData();
				//System.out.printf("id=%d, dist=%f\r\n", d.mNodeID, d.mDistance);
				
				LocalizationAlgorithm.instance().addData(d);
				
				Point p = LocalizationAlgorithm.instance().getPosition();
				
				System.out.println(p.toString());

				MainUI.card1.getMapPanel().drawLoc(p);
			}
		});
        
    }

    public static void main(String[] args) {

        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }


}
